// @formatter:off
/**
 * @file s.edittags.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-27 04:56:35 +0000 (Mon, 27 May 2013) $
 *
 * $Id: s.edittags.js 67 2013-05-27 04:56:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/themes/default/templates/simplecontainer/s.edittags.js $
 */
// @formatter:on

var title = "Edit <:VAR:/>EDIT-WHAT<::/> tags: '<:VAR:/>EDIT-NAME<::/>'";
var EDITTAGS_MODE = "ADD";
var autoTransID = 0;
var loadTransID = 0;

function SendTags() {
	var tags = $("#TAGS").val().trim().split(",");
	$("#EDITTAGS_LIST").append('<span class="WCTagButton WCSpecial" WC_href="#" WC_click="alert(\'updating...\');return false;">updating... </span>');
	jQuery.post('service.php?action=<:VAR:/>WC_SERVICE_TAGS<::/>&group=<:VAR:/>GROUP-ID<::/>&<:VAR:/>EDIT-URI-NAME<::/>=<:VAR:/>EDIT-ID<::/>', {
		'MODE': EDITTAGS_MODE,
		'LIST': tags
	}, function(data) {
		var xml = $($.parseXML(data));
		if(xml.find("wc-failed").length) {
			var error = "";
			if(xml.find("wc-error>wc-code").length) {
				error = "[" + xml.find("wc-error>wc-code").text() + "] ";
			}
			error = error + xml.find("wc-error>wc-message").text();

			alert("Failed: " + error);
		}
		loadTags();
	});
	$("#TAGS").val("");
}

function loadTags() {
	$("#EDITTAGS_LIST").append('<span class="WCTagButton WCSpecial" WC_href="#" WC_click="alert(\'loading...\');return false;">loading... </span>');
	loadTransID++;
	console.log("loadTags(): Sending transaction '" + loadTransID + "'");
	jQuery.get('service.php?action=<:VAR:/>WC_SERVICE_TAGS<::/>&group=<:VAR:/>GROUP-ID<::/>&<:VAR:/>EDIT-URI-NAME<::/>=<:VAR:/>EDIT-ID<::/>&transaction=' + loadTransID, function(data) {
		var xml = $($.parseXML(data));
		var transId = xml.find("wc-transaction").text();
		console.log("loadTags(): Setting results from transaction '" + transId + "'");
		if(transId == loadTransID) {
			console.log("loadTags(): Setting results from transaction '" + transId + "'");
			if(xml.find("wc-success").length) {
				$("#EDITTAGS_LIST").html("");
				xml.find("wc-result>tag").each(function() {
					var name = $(this).find("name").text();
					var special = false;

					if($(this).find("special").length) {
						special = true;
					}

					$("#EDITTAGS_LIST").append('<div class="TagButtonDELETE TagButton' + ( special ? ' TagButtonSPECIAL' : '') + '" TagButtonName="' + name + '"><a href="#" onclick="RemoveTag(\'' + name + '\')">' + name + '</a></div>');
				});
				$("#EDITTAGS_LIST").css({
					height: "80px"
				});
				TagButtons();
			} else if(xml.find("wc-failed").length) {
				var error = "";
				if(xml.find("wc-error>wc-code").length) {
					error = "[" + xml.find("wc-error>wc-code").text() + "] ";
				}
				error = error + xml.find("wc-error>wc-message").text();

				$("#EDITTAGS_LIST").text("Failed: " + error);
			}
		}
	});
}

function loadSelectableTags() {
	$("#TAGS").bind("keydown", function(event) {
		if(event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
			event.preventDefault();
		}
	}).autocomplete({
		source: function(request, response) {
			var pattern = request.term.split(/,\s*/).pop();
			if(pattern.length == 0) {
				return false;
			}

			console.log("loadSelectableTags(): Asking for pattern '" + pattern + "'");

			autoTransID++;
			console.log("loadSelectableTags(): Sending transaction '" + autoTransID + "'");
			jQuery.get("service.php?action=<:VAR:/>WC_SERVICE_TAGS<::/>&group=<:VAR:/>GROUP-ID<::/>&pattern=" + pattern + "&transaction=" + autoTransID, function(data) {
				var xml = $($.parseXML(data));
				var transId = xml.find("wc-transaction").text();
				console.log("loadSelectableTags(): Receiving transaction '" + transId + "'");
				if(transId == autoTransID) {
					console.log("loadSelectableTags(): Setting results from transaction '" + transId + "'");
					if(xml.find("wc-success").length) {
						var availableTags = Array();

						xml.find("wc-result>tag>name").each(function() {
							availableTags.push($(this).text());
						});
						console.log("loadSelectableTags(): tags received: '" + availableTags.length + "'");

						response(availableTags);
					} else if(xml.find("wc-failed").length) {
						//var	error = "";
						//if(xml.find("wc-error>wc-code").length) {
						//	error = "[" + xml.find("wc-error>wc-code").text() + "] ";
						//}
						//error = error + xml.find("wc-error>wc-message").text();

						//$("#EDITTAGS_LIST").text("Failed: " + error);
					}
				}
			});
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = this.value.split(/,\s*/);
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push(ui.item.value);
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
	});
}

function RemoveTag(name) {
	if(confirm("Are you sure you want to remove tag '" + name + "'?")) {
		$("#TAGS").val(name);
		EDITTAGS_MODE = 'REMOVE';
		$("#EditForm").submit();
	}
}

wcSimpleContainer.whenOpened(function(window) {
	window.title(title);
	window.height(320);
	window.width(800);

	loadTags();

	$("#TAGS").focus();
	$("#TAGS").bind("keyup", loadSelectableTags);
});
